﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.IO;
using HF.Software.ClassLib;

namespace HF.Software
{
    /// <summary>
    /// hef创建于2015.02.03
    /// 系统配置文件读写
    /// </summary>
    public partial class frmMain : Form
    {
        #region create

        public frmMain()
        {
            InitializeComponent();

            //移除选项卡事件
            this.tabShowFrm.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabShowFrm_MouseDown);
        }

        public frmMain(string _sBranch,string _sGoods)
            : this()
        {
            this.sBranch = _sBranch;
            this.sGoods = _sGoods;
        }

        /// <summary>
        /// 加载treeView节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmReadWriteIni_Load(object sender, EventArgs e)
        {
            try
            {
                if (!File.Exists(sIniPath))
                {
                    MessageBox.Show("对不起，程序配置文件不存在！");
                    return;
                }

                ClsIniFiles oIni = new ClsIniFiles(sIniPath);

                StringCollection oStrLst = null;

                oStrLst = oIni.fnReadSections(oStrLst);

                if (null == oStrLst || 0 == oStrLst.Count)
                {
                    MessageBox.Show("对不起，未能读取程序配置文件！");
                    return;
                }

                TreeNode oNode = this.tvwSet.Nodes[0];

                TreeNode oChildNode = null;

                for (int i = 0; i < oStrLst.Count; i++)
                {
                    oChildNode = new TreeNode();

                    switch (oStrLst[i].ToString().ToUpper())
                    {
                        case "SYS":
                            oChildNode.Text = "系统";
                            oChildNode.ImageIndex = 1;
                            oChildNode.SelectedImageIndex = 1;
                            break;
                        case "TIC":
                            oChildNode.Text = "票务";
                            oChildNode.ImageIndex = 2;
                            oChildNode.SelectedImageIndex = 2;
                            break;
                        case "MEM":
                            oChildNode.Text = "会员";
                            oChildNode.ImageIndex = 3;
                            oChildNode.SelectedImageIndex = 3;
                            break;
                        case "POS":
                            oChildNode.Text = "卖品";//前台
                            oChildNode.ImageIndex = 4;
                            oChildNode.SelectedImageIndex = 4;
                            break;
                        case "DOUBLESCREEN":
                            oChildNode.Text = "双屏";
                            oChildNode.ImageIndex = 5;
                            oChildNode.SelectedImageIndex = 5;
                            break;
                        case "SELF":
                            //2015.03.09hef注释
                            //oChildNode.Text = "自助机";
                            //oChildNode.ImageIndex = 6;
                            //oChildNode.SelectedImageIndex = 6;
                            break;
                        case "REPORT":
                            break;
                    }

                    if (string.IsNullOrWhiteSpace(oChildNode.Text)) continue;

                    oChildNode.Tag = oStrLst[i].ToString().ToUpper();

                    oNode.Nodes.Add(oChildNode);
                }

                this.tvwSet.SelectedNode = this.tvwSet.Nodes[0].Nodes[0];
            }
            catch (Exception ex)
            {
                this.oLog.fnLog("201502031337加载节点异常：" + ex.ToString());
            }

        }

        #endregion

        #region event

        /// <summary>
        /// 选项卡关闭按钮功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabShowFrm_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button != MouseButtons.Left) return;

                int x = e.X, y = e.Y;

                //计算关闭区域
                Rectangle oTabRect = this.tabShowFrm.GetTabRect(this.tabShowFrm.SelectedIndex);

                oTabRect.Offset(oTabRect.Width - (frmMain.CLOSE_SIZE + 3), 2);
                oTabRect.Width = frmMain.CLOSE_SIZE;
                oTabRect.Height = frmMain.CLOSE_SIZE;

                //如果鼠标在区域内就关闭选项卡
                bool bClose = x > oTabRect.X && x < oTabRect.Right && y > oTabRect.Y && y < oTabRect.Bottom;

                if (bClose)
                {
                    this.tabShowFrm.TabPages.Remove(this.tabShowFrm.SelectedTab);

                    if (this.tabShowFrm.TabPages.Count > 0)
                        this.tabShowFrm.SelectedTab = this.tabShowFrm.TabPages[this.tabShowFrm.TabPages.Count - 1];
                }
                else//点击不是关闭区域 选中选项卡关联节点
                {
                    return;
                }

                if (this.tabShowFrm.TabPages.Count == 0)//无选项卡 选中树根节点
                    this.tvwSet.SelectedNode = this.tvwSet.Nodes[0];
            }
            catch (Exception ex)
            {
                this.oLog.fnLog("201502031604关闭选项卡异常：" + ex.ToString());
            }
        }

        /// <summary>
        /// 选项卡选中treeView节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabShowFrm_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (null == this.tabShowFrm.SelectedTab) return;//判断是否为null 不判断移除选项卡会异常

            TreeNode oNodeRet = null;

            foreach (TreeNode oTn in this.tvwSet.Nodes)
            {
                oNodeRet = this.fnFindNode(oTn, this.tabShowFrm.SelectedTab.Text.Trim());

                if (oNodeRet != null) break;
            }

            if (null == oNodeRet) return;

            this.tvwSet.Focus();

            this.tvwSet.SelectedNode = oNodeRet;
        }

        /// <summary>
        /// treeView点击节点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvwSet_AfterSelect(object sender, TreeViewEventArgs e)
        {
            switch (this.tvwSet.SelectedNode.Text.Trim())
            {
                case "系统":
                    this.fnAddTabPageFrm(this.tvwSet.SelectedNode.Text, new frmSys());
                    break;
                case "票务":
                    this.fnAddTabPageFrm(this.tvwSet.SelectedNode.Text, new frmTic());
                    break;
                case "会员":
                    this.fnAddTabPageFrm(this.tvwSet.SelectedNode.Text, new frmMem());
                    break;
                case "卖品"://前台
                    #region 原代码 注释
                    
                    //clsOpaqueCommand oOpaque = null;
                    //try
                    //{
                    //    oOpaque = new clsOpaqueCommand();
                    //    oOpaque.ShowOpaqueLayer(this, 125, true);

                    //    if (this.eventShowBranch != null)
                    //        this.sBranch = this.eventShowBranch();

                    //    if (this.eventShowGoods != null)
                    //        this.sGoods = this.eventShowGoods();

                    //    oOpaque.HideOpaqueLayer();

                    //    oOpaque = null;
                    //}
                    //catch
                    //{
                    //    if (null != oOpaque)
                    //        oOpaque.HideOpaqueLayer();
                    //    oOpaque = null;
                    //}
                    #endregion
                    this.fnAddTabPageFrm(this.tvwSet.SelectedNode.Text, new frmPos(this.sBranch, this.sGoods));
                    break;
                case "双屏":
                    this.fnAddTabPageFrm(this.tvwSet.SelectedNode.Text, new frmDoubleScreen());
                    break;
                case "自助机":
                    //2015.03.09hef注释
                    //this.fnAddTabPageFrm(this.tvwSet.SelectedNode.Text, new frmSelf());
                    break;
                case "REPORT":
                    break;
            }
        }

        #endregion

        #region fun

        /// <summary>
        /// 选项卡内添加窗体
        /// </summary>
        /// <param name="_sTabPageName">选项卡名称</param>
        /// <param name="_oFrm">窗体</param>
        private void fnAddTabPageFrm(string _sTabPageName, Form _oFrm)
        {
            if (this.fnTabControlCheckHave(this.tabShowFrm, _sTabPageName)) return;

            TabPage oTabPage = new TabPage();
            oTabPage.Text = _sTabPageName;
            oTabPage.Name = _sTabPageName;
            oTabPage.ImageIndex = 0;

            this.tabShowFrm.TabPages.Add(oTabPage);

            this.tabShowFrm.SelectTab(tabShowFrm.TabPages.Count - 1);

            _oFrm.FormBorderStyle = FormBorderStyle.None;
            _oFrm.Dock = DockStyle.Fill;
            _oFrm.TopLevel = false;
            _oFrm.Show();
            _oFrm.Parent = tabShowFrm.SelectedTab;
        }

        /// <summary>
        /// 验证选项卡是否重复
        /// </summary>
        /// <param name="_oTab">选项卡控件</param>
        /// <param name="_sTabName">选项卡tabPage名称</param>
        /// <returns>返回bool</returns>
        private bool fnTabControlCheckHave(TabControl _oTab, string _sTabName)
        {
            for (int i = 0; i < _oTab.TabCount; i++)
            {
                if (_oTab.TabPages[i].Text == _sTabName)
                {
                    _oTab.SelectedIndex = i;
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 遍历TreeView节点
        /// </summary>
        /// <param name="_oTnParent">根节点</param>
        /// <param name="_sValue">节点Text值</param>
        /// <returns>返回节点</returns>
        private TreeNode fnFindNode(TreeNode _oTnParent, string _sValue)
        {

            if (_oTnParent == null) return null;

            if (_oTnParent.Text == _sValue) return _oTnParent;

            else if (_oTnParent.Nodes.Count == 0) return null;

            TreeNode oCurrentNode, oCurrentParNode;

            oCurrentParNode = _oTnParent;

            oCurrentNode = oCurrentParNode.FirstNode;

            while (oCurrentNode != null && oCurrentNode != _oTnParent)
            {

                while (oCurrentNode != null)
                {
                    if (oCurrentNode.Text == _sValue) return oCurrentNode;

                    else if (oCurrentNode.Nodes.Count > 0)
                    {
                        oCurrentParNode = oCurrentNode;
                        oCurrentNode = oCurrentNode.FirstNode;
                    }
                    else if (oCurrentNode != oCurrentParNode.LastNode)
                    {
                        oCurrentNode = oCurrentNode.NextNode;
                    }
                    else
                        break;
                }

                while (oCurrentNode != _oTnParent && oCurrentNode == oCurrentParNode.LastNode)
                {
                    oCurrentNode = oCurrentParNode;
                    oCurrentParNode = oCurrentParNode.Parent;
                }

                if (oCurrentNode != _oTnParent)
                    oCurrentNode = oCurrentNode.NextNode;

            }
            return null;
        }

        #endregion

        #region var

        //声明分店委托
        public delegate string dlgDisplayBranch();
        //声明分店事件
        public event dlgDisplayBranch eventShowBranch;
        //声明卖品委托
        public delegate string dlgDisplayGoods();
        //声明卖品事件
        public event dlgDisplayGoods eventShowGoods;

        private string sBranch = "";
        private string sGoods = "";

        private const int CLOSE_SIZE = 15;
        private string sIniPath = AppDomain.CurrentDomain.BaseDirectory + "Config.INI";
        private ClsLog oLog = new ClsLog(Application.StartupPath + @"\log\set\" + DateTime.Now.ToString("yyyyMMddHH") + ".txt");

        #endregion

    }
}
